package net.hn.hnms.biz.project.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.hn.hnms.biz.project.domain.ProjectBudget;
import net.hn.hnms.biz.project.domain.ProjectBudgetDetail;
import net.hn.hnms.biz.project.domain.ProjectBudgetFile;
import net.hn.hnms.biz.project.mapper.ProjectBudgetDetailMapper;
import net.hn.hnms.biz.project.mapper.ProjectBudgetFileMapper;
import net.hn.hnms.biz.project.mapper.ProjectBudgetMapper;
import net.hn.hnms.biz.project.request.ProjectBudgetReq;
import net.hn.hnms.biz.project.service.ProjectBudgetService;
import net.hn.hnms.biz.project.vo.ProjectBudgetVo;
import net.hn.hnms.sys.common.core.constant.Constants;
import net.hn.hnms.sys.common.core.domain.ResponsePage;
import net.hn.hnms.sys.common.core.exception.HuaNengException;
import net.hn.hnms.sys.common.mybatis.core.page.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ProjectBudgetServiceImpl implements ProjectBudgetService {
    @Autowired
    private ProjectBudgetMapper projectBudgetMapper;

    @Autowired
    private ProjectBudgetFileMapper projectBudgetFileMapper;

    @Autowired
    private ProjectBudgetDetailMapper projectBudgetDetailMapper;

    @Override
    @Transactional
    public int insert(ProjectBudgetReq projectBudgetReq) {
        ProjectBudget insertObj = new ProjectBudget();
        BeanUtil.copyProperties(projectBudgetReq, insertObj);
        int flag = projectBudgetMapper.insert(insertObj);
        List<ProjectBudgetFile> projectBudgetFileList = projectBudgetReq.getProjectBudgetFile();
        for(ProjectBudgetFile file : projectBudgetFileList){
            file.setProjectBudgetId(insertObj.getProjectBudgetId());
            projectBudgetFileMapper.insert(file);
        }
        List<ProjectBudgetDetail> projectBudgetDetailList = projectBudgetReq.getProjectBudgetDetail();
        for(ProjectBudgetDetail projectBudgetDetail : projectBudgetDetailList){
            projectBudgetDetail.setProjectBudgetId(insertObj.getProjectBudgetId());
            projectBudgetDetailMapper.insert(projectBudgetDetail);
        }
        return flag;
    }
    @Override
    public void updateDeleteByIds(List<String> idList) {
        projectBudgetMapper.updateDeleteByIds(idList);
    }

    @Override
    @Transactional
    public void updateById(ProjectBudgetReq projectBudgetReq) {
        ProjectBudget updateObj = new ProjectBudget();
        BeanUtil.copyProperties(projectBudgetReq, updateObj);
        int updateCount = projectBudgetMapper.updateById(updateObj);
        if (updateCount == 0) {
            throw new HuaNengException(Constants.FAIL, "数据不存在");
        }
        //更新文件
        projectBudgetFileMapper.deleteProjectBudgetFileById(projectBudgetReq.getProjectBudgetId());
        List<ProjectBudgetFile> projectBudgetFileList = projectBudgetReq.getProjectBudgetFile();
        for(ProjectBudgetFile file : projectBudgetFileList){
            file.setProjectBudgetId(projectBudgetReq.getProjectBudgetId());
            projectBudgetFileMapper.insert(file);
        }
        //更新明细
        projectBudgetDetailMapper.deleteProjectBudgetDetailById(projectBudgetReq.getProjectBudgetId());
        List<ProjectBudgetDetail> projectBudgetDetailList = projectBudgetReq.getProjectBudgetDetail();
        for(ProjectBudgetDetail projectBudgetDetail : projectBudgetDetailList){
            projectBudgetDetail.setProjectBudgetId(projectBudgetReq.getProjectBudgetId());
            projectBudgetDetailMapper.insert(projectBudgetDetail);
        }
    }

    @Override
    public ProjectBudgetVo selectDetailById(String id) {
        ProjectBudgetVo vo = new ProjectBudgetVo();
        ProjectBudget projectBudget = projectBudgetMapper.selectById(id);
        if (projectBudget == null) {
            throw new HuaNengException(Constants.FAIL, "数据不存在");
        }
        BeanUtil.copyProperties(projectBudget, vo);
        //文件列表
        List<ProjectBudgetFile> projectBudgetFileList = projectBudgetFileMapper.getProjectBudgetFileList(id);
        vo.setProjectBudgetFile(projectBudgetFileList);
        //详情列表
        List<ProjectBudgetDetail> projectBudgetDetailList = projectBudgetDetailMapper.getProjectBudgetDetailList(id);
        vo.setProjectBudgetDetail(projectBudgetDetailList);
        return vo;
    }

    @Override
    public ResponsePage<ProjectBudgetVo> selectPageList(ProjectBudgetReq projectBudgetReq, PageQuery pageQuery) {
//        Page<ProjectBudget> page = projectBudgetMapper.selectPage(pageQuery.build(), buildQuery(projectBudgetReq).orderByDesc(ProjectBudget::getCreateTime));
//        List<WaterSupplyCompanyVo> list =page.getRecords().stream().map(o ->convert(o, WaterSupplyCompanyVo.class)).collect(Collectors.toList());
//        return new ResponsePage<>(page.getRecords(), page.getTotal());
        Page<ProjectBudgetVo> page = projectBudgetMapper.selectPageList(pageQuery.build(), projectBudgetReq);
        return new ResponsePage<>(page.getRecords(), page.getTotal());
    }

    /**
     * 构造查询条件
     * @param req
     * @return LambdaQueryWrapper<ExecutionOther> 查询条件
     */
    private LambdaQueryWrapper<ProjectBudget> buildQuery(ProjectBudgetReq req){
        return new LambdaQueryWrapper<ProjectBudget>()
                .like(StrUtil.isNotBlank(req.getBudgetTitle()), ProjectBudget::getBudgetTitle, req.getBudgetTitle());
    }



}
